﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.DAO;
using ClassLibrary.Entity;

namespace mprsc
{
    public partial class ISSUELOANPHONE : Form
    {
        TblLoan2PhoneDAO Loan2PhoneDAO = new TblLoan2PhoneDAO(Global.CONNECTION_STRING);
        TblJobsDAO jobDAO = new TblJobsDAO(Global.CONNECTION_STRING);
        TblLoanPhoneDAO LoanPhoneDAO = new TblLoanPhoneDAO(Global.CONNECTION_STRING);

        public delegate void SendMessage(string Message);
        public SendMessage Sender; 

        public ISSUELOANPHONE()
        {
            InitializeComponent();

            //Tao con tro toi hàm GetMessage
            Sender = new SendMessage(GetMessage);

            LoadLoanPhone();
            LoadPaymentMethod();
        }

        //Hàm có nhiem vu lay tham so truyen vào
        private void GetMessage(string Message)
        {
            lbJobID.Text = Message;
        }

        private void ISSUELOANPHONE_Load(object sender, EventArgs e)
        {
            this.CenterToParent();
        }

        //Load Payment method
        private void LoadPaymentMethod()
        {
            var method = new Dictionary<int, string>();
            method.Add(1, "Cash");
            method.Add(2, "EFT");
            cbPaymentMethod.DataSource = new BindingSource(method, null);
            cbPaymentMethod.DisplayMember = "Value";
            cbPaymentMethod.ValueMember = "Value";
        }
        // Load Loan Phone
        private void LoadLoanPhone()
        {
            cbLPhoneID.DataSource = Loan2PhoneDAO.LoadIDLPhone();
            cbLPhoneID.DisplayMember = "LPhoneID";
            cbLPhoneID.ValueMember = "LPhoneID";
        }

        private void cbLPhoneID_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable L2Phone_dt = Loan2PhoneDAO.GetLPhoneByID(cbLPhoneID.SelectedValue.ToString());
            foreach (DataRow dr in L2Phone_dt.Rows)
            {
                txt_Brand.Text = dr["LPhoneBrand"].ToString();
                txt_Model.Text = dr["LPhoneModel"].ToString();
                txt_IMEI.Text = dr["LPIMENo"].ToString();
                txt_Included.Text = dr["AccessoriresIncluded"].ToString();
                txt_Amount.Text = dr["Deposit"].ToString();
                txt_LoanFree.Text = dr["LoanFee"].ToString();
            }
        }

        private void btnSaveLoanPhone_Click(object sender, EventArgs e)
        {
            bool Flag = false;
            if (txt_Brand.Text == "" ||  txt_Model.Text =="" || txt_Included.Text =="" || txt_IMEI.Text == "")
            {
                this.validationProvider1.ValidationMessages(!this.validationProvider1.Validate());
            }
            else
            {
                int id = Global.ConvertToInt(lbJobID.Text);
            
                try
                {
                    TblLoanPhone loanphone = new TblLoanPhone();

                    DataTable dtCus = jobDAO.GetJob(id, "");

                    foreach (DataRow r in dtCus.Rows)
                    {
                        loanphone.CustomerID = Global.ConvertToInt(r["CustomerIndex"].ToString());
                    }
                    loanphone.LPhoneID = cbLPhoneID.SelectedValue.ToString();
                    loanphone.Notes = txt_Notes.Text;
                    loanphone.PaymentMethod = cbPaymentMethod.Text;
                    loanphone.Brand = txt_Brand.Text;
                    loanphone.Model = txt_Model.Text;
                    loanphone.IMENo = txt_IMEI.Text;
                    loanphone.PaymentRef = "";

                    loanphone.LoanFee = Convert.ToDecimal(txt_LoanFree.Text);
                    loanphone.AccessoriesIncluded = txt_Included.Text;
                    loanphone.AmountDeposited = Convert.ToDecimal(txt_Amount.Text);

                    loanphone.DateIn = DateTime.Now;
                    loanphone.DateOut = DateTime.Now;
                    if (ck_Deposit.Checked == true)
                    {
                        loanphone.DepositeTaken = Convert.ToBoolean(1);

                    }
                    else
                    {
                        loanphone.DepositeTaken = Convert.ToBoolean(1);
                    }
                    if (cbDesRef.Checked == true)
                    {
                        loanphone.DepositeRefunded = Convert.ToBoolean(1);
                    }
                    else
                    {
                        loanphone.DepositeRefunded = Convert.ToBoolean(0);
                    }
                    loanphone.LoanID = 0;
                    loanphone.JobID = id;
                    LoanPhoneDAO.InsUpd_TblLoanPhone(loanphone, 1);
                    Loan2PhoneDAO.Update(1, cbLPhoneID.SelectedValue.ToString());
                    Flag = true;

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
             if (Flag)
             {
                 string id = lbJobID.Text;
                 this.Hide();
                 NewBooking booking = new NewBooking();
                 booking.Sender(id);
                 booking.Show();
             }
           
        }

       
    }
}
